﻿//TODO: Dejitter: Test

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// Tries to "de"jitter a jittered image
    /// </summary>
    public class Dejitter : BaseImageFilter
    {
        private int m_nWidth = 3;
        private int m_nHeight = 3;

        /// <summary>
        /// Initializes a new instance of the <see cref="Dejitter"/> class.
        /// </summary>
        public Dejitter()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Dejitter"/> class.
        /// </summary>
        /// <param name="_nWidth">Width of the _n.</param>
        /// <param name="_nHeight">Height of the _n.</param>
        public Dejitter(int _nWidth, int _nHeight)
        {
            m_nWidth = _nWidth;
            m_nHeight = _nHeight;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_dejitter"></param>
        internal Dejitter(Dejitter _dejitter)
        {
            this.m_nWidth = _dejitter.m_nWidth;
            this.m_nHeight = _dejitter.m_nHeight;
        }

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            int m_cnFilterWidth = 3;
            int m_ncFilterHeight = 3;
            UnsafeBitmap bitmap = _bitmap;
            
            int nWidth = bitmap.Width;
            int nHeight = bitmap.Height;

            int imageX = 0;
            int imageY = 0;
            int x = 0;
            int y = 0;
            int filterX = 0;
            int filterY = 0;

            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    for (filterX = 0; filterX < m_cnFilterWidth; filterX++)
                    {
                        for (filterY = 0; filterY < m_ncFilterHeight; filterY++)
                        {
                            imageX = (x - m_cnFilterWidth / 2 + filterX + nWidth) % nWidth;
                            imageY = (y - m_ncFilterHeight / 2 + filterY + nHeight) % nHeight;
                            Color color = bitmap.GetPixel(imageX, imageY);
                        }
                    }
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new Dejitter(this);
        }
    }
}
